package com.gz.DroidPong.Sprites;

import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.gz.DroidPong.Constants.TextureManager;
import com.gz.DroidPong.Constants.UIManager;

public class Cup extends Sprite{

	private float mThickness = 2;
	
	
	public Cup(float pX, float pY, float pWidth,PhysicsWorld pWorld, Scene pScene, int pLayerID) {
		super(pX, pY, pWidth, TextureManager.getHeightFromWidth(GetTR(),pWidth), GetTR());
		
		
		pScene.getChild(pLayerID).attachChild(this);
		SetWalls(pWorld, pScene, pLayerID);
		Sprite front = new Sprite(getX(),getY(),pWidth,getHeight(),UIManager.getTextures().getTexture(TextureManager.CUP_FRONT_BLACK_TEXTURE));
		front.setAlpha(.5f);
		pScene.attachChild(front);
	}

	private static TextureRegion GetTR(){
		return UIManager.getTextures().getTexture(TextureManager.CUP_BLACK_TEXTURE);
	}
	
	private Shape SetWalls(PhysicsWorld pWorld, Scene pScene, int pLayerID){
	
		final Shape floor = new Rectangle(getX() + .2f*getWidth(),getY() + getHeight() - mThickness - getHeight()/10,.6f*getWidth(),mThickness);
		final FixtureDef fd = PhysicsFactory.createFixtureDef(0, 0.0f, 1f);
		PhysicsFactory.createBoxBody(pWorld, floor, BodyType.StaticBody, fd);
		//this.attachChild(floor);
		
		
		final Shape wallL = new Rectangle(getX() + .15f*getWidth(), getY() + getHeight()/7, mThickness, 5*getHeight()/7);
		wallL.setRotation(-7);
		PhysicsFactory.createBoxBody(pWorld,wallL, BodyType.StaticBody, fd);
		
		final Shape wallR = new Rectangle(getX() + getWidth() - mThickness - getWidth()/7, getY() + getHeight()/7, mThickness, 5*getHeight()/7);
		wallR.setRotation(7);
		Body bodyR = PhysicsFactory.createBoxBody(pWorld, wallR, BodyType.StaticBody, fd);
		
		final FixtureDef lipFD = PhysicsFactory.createFixtureDef(0, 0.3f, .25f);
		
		final Shape lipL = new Rectangle(wallL.getX() - getWidth()/20,wallL.getY() - getWidth()/20,wallL.getWidth(), getWidth()/10);
		lipL.setRotation(-45);
		PhysicsFactory.createBoxBody(pWorld, lipL, BodyType.StaticBody, lipFD);
		
		final Shape lipR = new Rectangle(wallR.getX() + getWidth()/20, wallR.getY() - getWidth()/20, wallR.getWidth(), getWidth()/10);
		lipR.setRotation(45);
		PhysicsFactory.createBoxBody(pWorld, lipR, BodyType.StaticBody, lipFD);
		
		
		floor.setAlpha(0);
		wallL.setAlpha(0);
		wallR.setAlpha(0);
		lipL.setAlpha(0);
		lipR.setAlpha(0);
		
		pScene.getChild(pLayerID).attachChild(floor);
		pScene.getChild(pLayerID).attachChild(wallL);
		pScene.getChild(pLayerID).attachChild(wallR);
		pScene.getChild(pLayerID).attachChild(lipL);
		pScene.getChild(pLayerID).attachChild(lipR);
		return floor;
	}
	
}
